/**
 * 素材中心
 * */


(function () {
})();


//购物车列表
function getList(){
    var formData = {
        start: pages.start,
        limit: pages.limit
    };
    $.ajax({
        type: apis.qdSucaiIndex.method,
        url: apis.qdSucaiIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formArys = res.data;
            tbody.html(getHtml());
            $('#pageTotal').html(formArys.length);
            //pages.total = res.total;
            //pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//拼接字符串
function getHtml(){
    var d = formArys;
    var h = '', i, status;
    for (i = 0; i < d.length; i ++) {
        status = d[i].status;
        h += '<tr class="text-c">';
        h += '<td>' + d[i].gname + '</td>';
        h += '<td>';
        if (d[i].gimg == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].gPrefix + d[i].gimg + '" height="50"/>';
        }
        h += '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>';
        if (d[i].imgLink == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].imgPrefix + d[i].imgLink + '" height="50"/>';
        }
        h += '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="sucai_show(\'详情\',\'sucai-show.html\', ' + d[i].id + ');">';
        //h += '详情(上传)';
        h += '详情';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="sucai_edit(\'编辑\',\'sucai-edit.html\', ' + d[i].id + ');" style="text-decoration:none">';
        h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="sucai_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//抓图
function uploadImg() {
    var fileObj = $('#' + selid + 'Upload')[0], reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        imageUpload({type: type, img: res.target.result});
    }
}


//编辑
function sucai_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,400);
}


//编辑
function sucai_show(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(id){
    $.ajax({
        type: apis.qdSucaiOne.method,
        url: apis.qdSucaiOne.url,
        data: { id: id, },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                $('#gno')[0].value = '';
                $('#title')[0].value = '';
                $('#detail').html('');
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            $('#gno')[0].value = d.gno;
            $('#title')[0].value = d.title;
            $('#detail').html(d.detail);
            var imgList = d.imgList;
            if (imgList.length > 0) {
                var h = '', i;
                for (i = 0; i < imgList.length; i ++) {
                    h += '<img src="' + imgList[i].imgPrefix + imgList[i].imgLink + '"/>';
                }
                $('#bigShow').html(h);
            }
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//获取详情
function showOne(id){
    $.ajax({
        type: apis.qdSucaiOne.method,
        url: apis.qdSucaiOne.url,
        data: { id: id, },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            $('#gno').html(d.gno);
            $('#title').html(d.title);
            $('#detail').html(d.detail);
            var imgList = d.imgList;
            if (imgList.length > 0) {
                var h = '', i;
                for (i = 0; i < imgList.length; i ++) {
                    h += '<img src="' + imgList[i].imgPrefix + imgList[i].imgLink + '"/>';
                }
                $('#bigShow').html(h);
            }
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//显示主图
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).width(e.w).height(e.h);
}


//保存更新
function modify(){
    var formData = {
        id: id,
        gno: $('#gno').val(),
        title: $('#title').val(),
        detail: $('#detail').val(),
    };
    if (!formData.title) {
        msgPop({msg: '素材标题必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.qdSucaiModify.method,
        url: apis.qdSucaiModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//图片上传
function imageUpload(e){
    var formData = {
        scid: id, img: e.img,
    };
    //console.log(formData);
    //return
    $.ajax({
        type: apis.qdSucaiUpload.method,
        url: apis.qdSucaiUpload.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layer.msg(res.msg,{icon: 1,time: 1000});
                return
            }
            msgPop({msg: '上传成功'});
            setTimeout(function(){
                //top.location.reload();
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//删除
function sucai_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.qdSucaiDel.method,
            url: apis.qdSucaiDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}